import logging
import logging.config
import json

# JSON 配置
logging_config = {
    "version": 1,
    "formatters": {
        "default": {
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        }
    },
    "handlers": {
        "file_all": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "DEBUG",
            "formatter": "default",
            "filename": "all_logs.log",
            "mode": "a",
            "maxBytes": 10485760,
            "backupCount": 5,
            "encoding": "utf-8"
        },
        "file_error": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "ERROR",
            "formatter": "default",
            "filename": "error_logs.log",
            "mode": "a",
            "maxBytes": 10485760,
            "backupCount": 5,
            "encoding": "utf-8"
        },
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "default",
            "stream": "ext://sys.stdout"
        }
    },
    "loggers": {
        "monitor": {
            "level": "DEBUG",
            "handlers": ["file_all", "file_error", "console"],
            "propagate": False
        },
        "mqtt": {
            "level": "DEBUG",
            "handlers": ["file_all", "file_error", "console"],
            "propagate": False
        },
        "ui": {
            "level": "DEBUG",
            "handlers": ["file_all", "file_error", "console"],
            "propagate": False
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": []
    }
}

# 配置日志
logging.config.dictConfig(logging_config)

# 创建日志器
monitor_logger = logging.getLogger('monitor')
# mqtt_logger = logging.getLogger('mqtt')
# ui_logger = logging.getLogger('ui')

# 记录日志
monitor_logger.info('monitor 日志配置成功')
# mqtt_logger.info('MQTT 日志配置成功')
# ui_logger.info('UI 日志配置成功')
